package.path = "./plugin/TQYJ/utils.lua"..";..\\?.lua"
package.path = "./plugin/TQYJ/json.lua"..";..\\?.lua"

require "utils.lua"
require "json.lua"

rxBuf = {}
regAddr = 0

function NewVariables()
    -- body
    local VariableMap = {
        Variable = {
        },
    }

    return VariableMap
end

function GenerateGetRealVariables(sAddr,step)

    -- body
    local crc
    local requestADU = {}
    local VariableMap = {
        Status = "0",
        Variable = {},
    }

    if(step == 0)
    then
        ----------读运行参数-----------------
        RegAddr = 0
        RegCnt = 8
        requestADU[1] = sAddr
        requestADU[2] = 0x01
        requestADU[3] = math.modf(RegAddr/256)
        requestADU[4] = math.modf(RegAddr%256)
        requestADU[5] = math.modf(RegCnt/256)
        requestADU[6] = math.modf(RegCnt%256)
        local VariableMapTemp = {
            Variable = requestADU
        }
        crc = GetCRCModbus(VariableMapTemp)
        requestADU[7] = math.modf(crc%256)
        requestADU[8] = math.modf(crc/256)

        regAddr = 0
        VariableMap = {
            Status = "0",
            Variable = requestADU
        }
    end


    return VariableMap
end

function DeviceCustomCmd(sAddr,cmdName,cmdParam,step)

    -- body
    local crc
    local requestADU = {}
    local VariableMap = {
        Status = "0",
        Variable = {},
    }
    if(cmdName == "GetRealVariables")
    then
        if(step == 0)
        then
            ----------读运行参数-----------------
            RegAddr = 0
            RegCnt = 8
            requestADU[1] = sAddr
            requestADU[2] = 0x01
            requestADU[3] = math.modf(RegAddr/256)
            requestADU[4] = math.modf(RegAddr%256)
            requestADU[5] = math.modf(RegCnt/256)
            requestADU[6] = math.modf(RegCnt%256)
            local VariableMapTemp = {
                Variable = requestADU
            }
            crc = GetCRCModbus(VariableMapTemp)
            requestADU[7] = math.modf(crc%256)
            requestADU[8] = math.modf(crc/256)

            regAddr = 0
            VariableMap = {
                Status = "0",
                Variable = requestADU
            }
        end
    elseif(cmdName == "SetVariables")
    then
        res = json.jsondecode(cmdParam)
        if (res["Relay1"] ~= nil)
        then
            requestADU = WriteRelay(sAddr,0x00,res["Relay1"])
            regAddr = 0x0000
            VariableMap = {
                Status = "0",
                Variable = requestADU
            }
        elseif (res["Relay2"] ~= nil)
        then
            requestADU = WriteRelay(sAddr,0x01,res["Relay2"])
            regAddr = 0x0001
            VariableMap = {
                Status = "0",
                Variable = requestADU
            }
        elseif (res["Relay3"] ~= nil)
        then
            requestADU = WriteRelay(sAddr,0x02,res["Relay3"])
            regAddr = 0x0002
            VariableMap = {
                Status = "0",
                Variable = requestADU
            }
        elseif (res["Relay4"] ~= nil)
        then
            requestADU = WriteRelay(sAddr,0x03,res["Relay4"])
            regAddr = 0x0003
            VariableMap = {
                Status = "0",
                Variable = requestADU
            }
        elseif (res["Relay5"] ~= nil)
        then
            requestADU = WriteRelay(sAddr,0x04,res["Relay5"])
            regAddr = 0x0004
            VariableMap = {
                Status = "0",
                Variable = requestADU
            }
        elseif (res["Relay6"] ~= nil)
        then
            requestADU = WriteRelay(sAddr,0x05,res["Relay6"])
            regAddr = 0x0005
            VariableMap = {
                Status = "0",
                Variable = requestADU
            }
        elseif (res["Relay7"] ~= nil)
        then
            requestADU = WriteRelay(sAddr,0x06,res["Relay7"])
            regAddr = 0x0006
            VariableMap = {
                Status = "0",
                Variable = requestADU
            }
        elseif (res["Relay8"] ~= nil)
        then
            requestADU = WriteRelay(sAddr,0x07,res["Relay8"])
            regAddr = 0x0007
            VariableMap = {
                Status = "0",
                Variable = requestADU
            }
        end
    end

    return VariableMap
end

function WriteRelay(devAddr,reaAddr,regValue)
    local requestADU= {}

    requestADU[1] = tonumber(devAddr)
    requestADU[2] = 0x05
    requestADU[3] = 0x00
    requestADU[4] = reaAddr
    if(regValue == 0 or regValue == "0") then
        requestADU[5] = 0x00
        requestADU[6] = 0x00
    elseif(regValue == 1 or regValue == "1") then
        requestADU[5] = 0xFF
        requestADU[6] = 0x00
    end
    local VariableMapTemp = {
        Variable = requestADU
    }
    crc = GetCRCModbus(VariableMapTemp)
    requestADU[7] = math.modf(crc%256)
    requestADU[8] = math.modf(crc/256)

    return requestADU
end

function AnalysisRx(sAddr,rxBufCnt)

    local index = 1
    local mAddr = 0
    local mFunCode = 0
    local mRegByte = 0
    local crcH,crcL,crc,crcTemp

    -- body
    local VariableMap = {
        Status = "1",
        Variable = {},
    }

    --modbus03 06 最短为8个字节
    if(rxBufCnt < 6)
    then
        rxBuf = {}
        return VariableMap
    end


    for index=1,rxBufCnt,1 do
        if(index <= rxBufCnt)
        then
            mAddr     = rxBuf[index]
            mFunCode  = rxBuf[index+1]
            if (mAddr == tonumber(sAddr))
            then
                if(mFunCode == 0x03)
                then
                    mRegByte = rxBuf[index+2]
                    if(mRegByte+5 > rxBufCnt)
                    then
                        rxBuf = {}
                        return VariableMap
                    end
                    --取出数据域数据
                    local pdu = {}
                    for i=index,index+2+mRegByte,1 do
                        if rxBuf[i] ~= nil then
                            table.insert(pdu, rxBuf[i])
                        end
                    end
                    local VariableMapCRC = {
                        Variable = pdu
                    }
                    crc = GetCRCModbus(VariableMapCRC)
                    crcTemp = rxBuf[index+3+mRegByte+1]*256+rxBuf[index+3+mRegByte]
                    if(crc == crcTemp)
                    then
                        local Variable = {}
                        local varIndex = 3
                        local VariableIndex = 1
                        if(regAddr == 40022)
                        then
                            VariableMap = {
                                Status = "0",
                                Variable = Variable
                            }
                        end
                        rxBuf = {}
                        return VariableMap
                    end
                elseif(mFunCode == 0x01 or mFunCode == 0x02)
                    then
                        mRegByte = rxBuf[index+2]
                        if(mRegByte+5 > rxBufCnt)
                        then
                            rxBuf = {}
                            return VariableMap
                        end
                        --取出数据域数据
                        local pdu = {}
                        for i=index,index+2+mRegByte,1 do
                            if rxBuf[i] ~= nil then
                                table.insert(pdu, rxBuf[i])
                            end
                        end
                        local VariableMapCRC = {
                            Variable = pdu
                        }
                        crc = GetCRCModbus(VariableMapCRC)
                        crcTemp = rxBuf[index+3+mRegByte+1]*256+rxBuf[index+3+mRegByte]
                        if(crc == crcTemp)
                        then
                            local Variable = {}
                            local varIndex = 3
                            local VariableIndex = 1
                            if(regAddr == 0)
                            then
                                ---------------------------继电器----------------------------------
                                local relay = rxBuf[index+varIndex]
                                varIndex = varIndex + 1

                                Variable[VariableIndex] = UpdateAlarm(relay,0x0001,VariableIndex,"Relay1")
                                VariableIndex = VariableIndex + 1

                                Variable[VariableIndex] = UpdateAlarm(relay,0x0002,VariableIndex,"Relay2")
                                VariableIndex = VariableIndex + 1

                                Variable[VariableIndex] = UpdateAlarm(relay,0x0004,VariableIndex,"Relay3")
                                VariableIndex = VariableIndex + 1

                                Variable[VariableIndex] = UpdateAlarm(relay,0x0008,VariableIndex,"Relay4")
                                VariableIndex = VariableIndex + 1

                                Variable[VariableIndex] = UpdateAlarm(relay,0x0010,VariableIndex,"Relay5")
                                VariableIndex = VariableIndex + 1

                                Variable[VariableIndex] = UpdateAlarm(relay,0x0020,VariableIndex,"Relay6")
                                VariableIndex = VariableIndex + 1

                                Variable[VariableIndex] = UpdateAlarm(relay,0x0040,VariableIndex,"Relay7")
                                VariableIndex = VariableIndex + 1

                                Variable[VariableIndex] = UpdateAlarm(relay,0x0080,VariableIndex,"Relay8")
                                VariableIndex = VariableIndex + 1
                                -------------------------------------------------------------
                                VariableMap = {
                                    Status = "0",
                                    Variable = Variable
                                }
                            end
                            rxBuf = {}
                            return VariableMap
                        end
                elseif(mFunCode == 0x05)
                then
                    mRegAddr = rxBuf[index+2]*256+rxBuf[index+3]
                    --取出数据域数据
                    local pdu = {}
                    for i=index,index+5,1 do
                        if rxBuf[i] ~= nil then
                            table.insert(pdu, rxBuf[i])
                        end
                    end
                    local VariableMapCRC = {
                        Variable = pdu
                    }
                    crc = GetCRCModbus(VariableMapCRC)
                    crcTemp = rxBuf[index+6+1]*256+rxBuf[index+6]
                    if(crc == crcTemp)
                    then
                        VariableMap = {
                            Status = "0",
                            Variable = Variable
                        }
                        rxBuf = {}
                        return VariableMap
                    end
                elseif(mFunCode == 0x06)
                then
                    mRegAddr = rxBuf[index+2]*256+rxBuf[index+3]
                    --取出数据域数据
                    local pdu = {}
                    for i=index,index+5,1 do
                        if rxBuf[i] ~= nil then
                            table.insert(pdu, rxBuf[i])
                        end
                    end
                    local VariableMapCRC = {
                        Variable = pdu
                    }
                    crc = GetCRCModbus(VariableMapCRC)
                    crcTemp = rxBuf[index+6+1]*256+rxBuf[index+6]
                    if(crc == crcTemp)
                    then
                        VariableMap = {
                            Status = "0",
                            Variable = Variable
                        }
                        rxBuf = {}
                        return VariableMap
                    end    
                else
                    index = index + 1
                end
            else
                index = index + 1
            end
        else
            rxBuf = {}
            return VariableMap
        end
    end

    rxBuf = {}
    return VariableMap
end

function UpdateAlarm(alarm,value,VariableIndex,name)
    local Variable

    if(utils.And(alarm,value) == 0x0000) then
        Variable = utils.AppendVariable(VariableIndex,name,0,"0")
    else
        Variable = utils.AppendVariable(VariableIndex,name,1,"1")
    end

    return Variable
end
